package com.atguigu.dga230315.meta.service.impl;

import com.atguigu.dga230315.meta.bean.TableMetaInfo;
import com.atguigu.dga230315.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga230315.meta.constant.MetaConst;
import com.atguigu.dga230315.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga230315.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author zhangjunyi
 * @since 2023-07-17
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {

    @Override
    public void genExtraListByMetaList(List<TableMetaInfo> tableMetaInfoList) {
        //问题：每天进行调度的时候 如何能够确保这个表对应的辅助信息只初始化一次？
        //解决方式:判断这个表是否已经初始化过辅助信息 如果没有的话那么再初始化    问题:如何知道这个表是否已经初始化过辅助信息
        //解决方式:根据表名和库名去辅助信息表里面查询，每行数据就是对应一张表的辅助信息 对应一个bean对象，如果这个对象是null则证明这个表没有初始化过辅助信息

        ArrayList<TableMetaInfoExtra> tableMetaInfoExtras = new ArrayList<>();
        //1.先获取到每张表
        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            TableMetaInfoExtra tableMetaInfoExtra = getOne(new QueryWrapper<TableMetaInfoExtra>().eq("table_name", tableMetaInfo.getTableName()).eq("schema_name", tableMetaInfo.getSchemaName()));

            //2.判断这个对象是否为null 如果是null证明这个表没有初始化辅助信息
            if (tableMetaInfoExtra==null){
                tableMetaInfoExtra = new TableMetaInfoExtra();

                //3.初始化辅助信息
                //表名&库名
                tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
                tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());

                //存储周期类型
                tableMetaInfoExtra.setLifecycleType(MetaConst.LIFECYCLE_TYPE_UNSET);

                //安全级别
                tableMetaInfoExtra.setSecurityLevel(MetaConst.SECURITY_LEVEL_UNSET);

                //数仓所在层级 根据表名判断
                String tableName = tableMetaInfo.getTableName();
               /* String tableName = tableMetaInfo.getTableName();
                if (tableName.startsWith("ods")){

                } else if (tableName.startsWith("dwd")) {

                }else {
                    other
                }*/

               String dwLevel= getInitDwLevelByTableName(tableName);
               tableMetaInfoExtra.setDwLevel(dwLevel);

               tableMetaInfoExtra.setCreateTime(new Date());

                tableMetaInfoExtras.add(tableMetaInfoExtra);
            }
        }

        saveOrUpdateBatch(tableMetaInfoExtras);

    }

    private String getInitDwLevelByTableName(String tableName) {
        if(tableName.startsWith("ods")){
            return MetaConst.DW_LEVEL_ODS;
        } else if (tableName.startsWith("dwd")) {
            return MetaConst.DW_LEVEL_DWD;
        }else if (tableName.startsWith("dim")) {
            return MetaConst.DW_LEVEL_DIM;
        }else if (tableName.startsWith("dws")) {
            return MetaConst.DW_LEVEL_DWS;
        }else if (tableName.startsWith("ads")) {
            return MetaConst.DW_LEVEL_ADS;
        }else if (tableName.startsWith("dm")) {
            return MetaConst.DW_LEVEL_DM;
        }else  {
            return MetaConst.DW_LEVEL_OTHER;
        }
    }
}
